package com.wsjc.web.controller.data;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.common.core.entity.ApiPageListResultVo;
import com.wsjc.common.core.entity.PageListVO;
import com.wsjc.common.core.page.PageBean;
import com.wsjc.common.core.page.PageParam;
import com.wsjc.common.core.utils.DateUtils;
import com.wsjc.common.core.utils.StringUtil;
import com.wsjc.manager.service.rpc.data.WjCollectionDataService;

@Controller
@RequestMapping(value = "/boss/data/")
public class WjDataController {

	@Autowired
	private WjCollectionDataService wjCollectionDataService ;
		
	private Logger logger = LoggerFactory.getLogger(WjDataController.class);
	
	
	/**
	 * 数据检索
	 * @param dataType
	 * @param hospitalName
	 * @param hospitalId
	 * @param hospitalType
	 * @param startTime
	 * @param endTime
	 * @param scope
	 * @param detectionResult
	 * @param page
	 * @param pageSize
	 * @param model
	 * @param location 请求位置：详情、数据检索
	 * @return
	 */
	@RequestMapping(value = "searchData", method = RequestMethod.GET)
	public String searchData(String location, String dataType, String hospitalName, String hospitalId, String hospitalType, String startTime, String endTime, String scope, String detectionResult,
			@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, Model model) {
		
		PageBean pageBean = null ;
		try {
			Map<String, Object> params = Maps.newHashMap();
			if(!StringUtil.isEmpty(hospitalName)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_NAME, hospitalName);
			}
			if(!StringUtil.isEmpty(hospitalId)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_ID, hospitalId);
			}
			if(!StringUtil.isEmpty(hospitalType)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_TYPE, hospitalType);
			}
			if(!StringUtil.isEmpty(scope)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_SCOPE, scope);
			}
			if(!StringUtil.isEmpty(detectionResult)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_DETECTIONRESULT, detectionResult);
			}
			if(!StringUtil.isEmpty(startTime) && !StringUtil.isEmpty(endTime)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_START_TIME, String.valueOf(DateUtils.StringToDate(startTime).getTime()));
				params.put(CommonConst.SEARCH_DATA_CONDITION_END_TIME, String.valueOf(DateUtils.StringToDate(endTime).getTime()));
			}

			//全国、省走solr , 市区 走db
			if(!StringUtil.isEmpty(scope)){
				String level = wjCollectionDataService.obtainScopeLevel(scope) ;
				if(level.equals(CommonConst.SCOPE_LEVEL_COUNTRY) || level.equals(CommonConst.SCOPE_LEVEL_PROVINCE)){
					pageBean = wjCollectionDataService.search(params, page, pageSize, dataType);
					//TODO 临时处理，bug：页码大于总页数时也会返回数据
					long totalPage = PageBean.countTotalPage(pageBean.getTotalCount(), pageBean.getNumPerPage()); // 最大页数
					if(page>totalPage){
						pageBean.setRecordList(null);
					}
					model.addAttribute("pageBean", pageBean);
                    return "页面 地址" ;
				}
			}
			PageParam pageParam = new PageParam(page,pageSize);
			pageBean = wjCollectionDataService.listDataPage(pageParam, params, scope, dataType);
			//TODO 临时处理，bug：页码大于总页数时也会返回数据
			long totalPage = PageBean.countTotalPage(pageBean.getTotalCount(), pageBean.getNumPerPage()); // 最大页数
			if(page>totalPage){
				pageBean.setRecordList(null);
			}
		} catch (Exception e) {
			logger.error("boss-data|searchData hapend error..", e);
		}
		model.addAttribute("pageBean", pageBean);
		return "页面 地址" ;
		
	}

							 
	
	/**
	 * 从solr 检索医院
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchHospital", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ApiPageListResultVo searchHospital(String dataType, String hospitalName, String detectionResult, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize) throws Exception {
		
		PageBean pageBean = null ;
		try {
			Map<String, Object> params = Maps.newHashMap();
			if(!StringUtil.isEmpty(hospitalName)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_HOSPITAL_NAME, hospitalName);
			}
			if(!StringUtil.isEmpty(detectionResult)){
				params.put(CommonConst.SEARCH_DATA_CONDITION_DETECTIONRESULT, detectionResult);
			}
			
			PageParam pageParam = new PageParam(page,pageSize);
			pageBean = wjCollectionDataService.search(params, page, pageSize, dataType);
			
			//TODO 临时处理，bug：页码大于总页数时也会返回数据
			long totalPage = PageBean.countTotalPage(pageBean.getTotalCount(), pageBean.getNumPerPage()); // 最大页数
			if(page>totalPage){
				pageBean.setRecordList(null);
			}
			ApiPageListResultVo vo = new ApiPageListResultVo();
			vo.setCode(0);
			vo.setMsg("");
			vo.setData(new PageListVO(pageBean.getTotalCount(), pageBean.getCurrentPage(), pageBean.getNumPerPage(), pageBean.getRecordList()));
			return vo;
		}catch (Exception e) {
			logger.error("boss-data|searchHospital hapend error..", e);
			ApiPageListResultVo vo = new ApiPageListResultVo();
			vo.setCode(-1);
			vo.setMsg("系统异常");
			return vo;
		}
	}



	
	
	
	
	
	
	
	
	

}
